package dimension.starry.block.machine.industry.ArmorTable;

import dimension.starry.StarryDimension;
import dimension.starry.block.machine.industry.IndustryMachineRegister;
import dimension.starry.block.machine.simple.SimpleMachineRegister;
import dimension.starry.block.machine.simple.weaponTable.WeaponTableContainer;
import dimension.starry.block.typeInterface.MachineTileEntity;
import dimension.starry.constants.ArmorExp;
import dimension.starry.constants.ItemQuality;
import dimension.starry.item.ItemRegisterManager;
import dimension.starry.item.typeInterface.ArmorType;
import dimension.starry.item.typeInterface.WeaponType.WeaponType;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class ArmorTableTileEntity extends MachineTileEntity {

    private static final Text TITLE = Text.translatable("block." + StarryDimension.MOD_ID + ".armor_table");

    public ArmorTableTileEntity(BlockPos blockPos, BlockState blockState) {
        super(IndustryMachineRegister.ARMOR_TABLE.getBlockEntityType(), blockPos, blockState, 3, TITLE, 800);
    }

    @Override
    public ScreenHandler createMenu(int sycID, PlayerInventory inventory, PlayerEntity player) {
        return new ArmorTableContainer(
            IndustryMachineRegister.ARMOR_TABLE.getScreenHandlerType(), sycID, inventory, getPos());
    }

    private int getExperiencePoints(ItemStack itemStack) {
        Item item = itemStack.getItem();
        if (item instanceof ArmorType armorType) {
            int total = 0;
            for (int i = 0; i <= ArmorType.getLevel(itemStack); i++) {
                total += ArmorExp.getNeedExp(armorType, i - 1);
            }
            return (int) (total * 0.8);
        }
        return 0;
    }

    @Override
    public void tick(World world, BlockPos pos, BlockState state1, MachineTileEntity blockEntity) {
        if (world == null || world.isClient()) {
            return;
        }
        ItemStack armorItemStack = this.inventory.getStack(0);
        ItemStack inputItemStack = this.inventory.getStack(1);

        if (armorItemStack.isEmpty() || inputItemStack.isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!(armorItemStack.getItem() instanceof ArmorType)) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (getExperiencePoints(inputItemStack) == 0) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (!this.inventory.getStack(2).isEmpty()) {
            this.setMachineStatus(false);
            this.resetProgress();
        } else if (ArmorType.getLevel(armorItemStack) < ArmorType.getMaxLevel(armorItemStack.getItem())) {
            ArmorType.addExp(armorItemStack, getExperiencePoints(inputItemStack));
            inputItemStack.setCount(inputItemStack.getCount() - 1);
            this.inventory.setStack(0, ItemStack.EMPTY);
            this.inventory.setStack(2, armorItemStack);
            this.resetProgress();
        }
    }


}
